<template>
  <el-dialog
    :close-on-click-modal='false'
    :title='dialog.title'
    :visible.sync='dialog.visible'
  >
    <el-form ref='dataForm' :model='dataForm' :rules='dataRule' label-width='120px'
             @keyup.enter.native='dataFormSubmit()'>
      <el-form-item label='参数名' prop='paramKey'>
        <el-input v-model='dataForm.paramKey' placeholder='参数名' />
      </el-form-item>
      <el-form-item label='参数值' prop='paramValue'>
        <el-input v-model='dataForm.paramValue' placeholder='参数值' />
      </el-form-item>
      <el-form-item label='备注' prop='remark'>
        <el-input v-model='dataForm.remark' placeholder='备注' />
      </el-form-item>
    </el-form>
    <span slot='footer' class='dialog-footer'>
      <el-button @click='cancel'>取消</el-button>
      <el-button type='primary' @click='handleSubmit'>确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { add, detail, update } from '@/api/admin/sysConfig'

export default {
  data() {
    return {
      visible: false,
      // 弹出层
      dialog: {
        title: undefined,
        visible: false,
        type: undefined // type 操作类型：1-新增 2-修改
      },
      dataForm: {
        id: 0,
        paramKey: '',
        paramValue: '',
        remark: ''
      },
      dataRule: {
        paramKey: [
          { required: true, message: '参数名不能为空', trigger: 'blur' }
        ],
        paramValue: [
          { required: true, message: '参数值不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init(id) {
      this.reset()
      if (id) {
        this.dialog = {
          title: '修改',
          visible: true,
          type: 'edit'
        }
        detail(id).then(({ data }) => {
          this.dataForm = data
        })
      } else {
        this.dialog = {
          title: '新增',
          visible: true,
          type: 'add'
        }
      }
    },
    handleSubmit() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          if (this.dialog.type === 'edit') {
            update(this.dataForm).then(() => {
              this.$message.success('修改成功')
              this.closeDialog()
              this.$emit('refreshDataList')
            })
          } else {
            add(this.dataForm).then(() => {
              this.$message.success('新增成功')
              this.closeDialog()
              this.$emit('refreshDataList')
            })
          }
        }
      })
    },
    // 取消按钮
    cancel() {
      this.reset()
      this.closeDialog()
    },
    reset() {
      this.dataForm = {
        id: undefined,
        paramKey: '',
        paramValue: '',
        remark: ''
      }
      this.resetForm('dataForm')
    },
    resetForm(val) {
      if (this.$refs[val]) {
        this.$refs[val].resetFields()
      }
    },
    closeDialog() {
      this.dialog = {
        title: undefined,
        visible: false,
        type: undefined
      }
    }
  }
}
</script>
